
MAJOR_VER=2
MINOR_VER=0

TOP_DIR = $(shell pwd)

AS=$(CROSS_COMPILE)as
CC=$(CROSS_COMPILE)gcc
LD=$(CROSS_COMPILE)ld 
OBJDUMP=$(CROSS_COMPILE)objdump
OBJCOPY=$(CROSS_COMPILE)objcopy


CFLAGS=-I$(TOP_DIR)/include -ffreestanding -nostdinc -nostdlib -fno-builtin -DGBIOS_VER_MAJOR=$(MAJOR_VER) -DGBIOS_VER_MINOR=$(MINOR_VER) -mno-thumb-interwork  -march=$(ARCH_VER) -mabi=aapcs-linux -DGBIOS_BH_START=$(CONF_BH_START) -O2 -mpoke-function-name

BUILD_YEAR  = $(shell date +%y)
BUILD_MONTH = $(shell date +%m)
BUILD_DAY   = $(shell date +%d)
HOUR = $(shell date +%H)
MIN  = $(shell date +%M)
SEC  = $(shell date +%S)


CFLAGS += -DBUILD_TIME=\"$(BUILD_YEAR)-$(BUILD_MONTH)-$(BUILD_DAY)\ $(HOUR):$(MIN):$(SEC)\"

# -mtune=

ASFLAGS = $(CFLAGS) -D__ASSEMBLY__

BUILT_IN_OBJ=built-in.o
LDFLAGS=-m armelf_linux_eabi

#PLAT_LIST = s3c2410 at91sam9261

export ARCH ARCH_VER SOC SOC_VENDOR PLAT CC LD CFLAGS LDFLAGS ASFLAGS CROSS_COMPILE BUILT_IN_OBJ TOP_DIR IMAGE_OUT_DIR

BH_SUBDIRS = \
	arch/$(ARCH) \
	mm \
	thread \
	device \
	lib \
	filesys \
	app

SUB_BUILT_IN_OBJS := $(foreach n, $(BH_SUBDIRS), $(n)/$(BUILT_IN_OBJ))


all: img_dir th bh
	@echo
	@ls -lh $(IMAGE_OUT_DIR)/g-bios*.bin
	@echo

img_dir:
	@mkdir -vp $(IMAGE_OUT_DIR)

th:
	@make -C boot all

bh: $(BH_SUBDIRS) g-bios-bh.bin $(IMAGE_OUT_DIR)/g-bios-bh.bin
	
g-bios-bh.elf: $(SUB_BUILT_IN_OBJS)
	$(LD) $(LDFLAGS) -T arch/$(ARCH)/$(SOC_VENDOR)/g-bios-bh.lds -Ttext $(CONF_BH_START) $(SUB_BUILT_IN_OBJS) -o g-bios-bh.elf

g-bios-bh.bin: g-bios-bh.elf
	$(OBJCOPY) -O binary -S $< $@
	@cp -v g-bios-bh.bin $(IMAGE_OUT_DIR)

$(IMAGE_OUT_DIR)/g-bios-bh.bin:
	@cp -v g-bios-bh.bin $(IMAGE_OUT_DIR)

clean:
	@for dir in boot $(BH_SUBDIRS); do \
		make -C $$dir clean; \
	 done	
	@rm -f *.bin *.elf

distclean: clean
	@rm -vf include/autoconf.h boot/kermit.c Makefile

#$(PLAT_LIST):
#	@rm -f .config
#	@cp -v arch/arm/$@/config.mk .config
#	@rm -f include/arch/soc.h
#	@ln -vs $@.h include/arch/soc.h
#	@echo

$(BH_SUBDIRS):
	@make -C $@ all

.PHONY: $(BH_SUBDIRS)

help:
	@cat readme.txt
	@echo

